package com.itheima.d2_udp.no2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

//目标：使用UDP通信实现多收消息
public class ReceiveDemo02 {
    public static void main(String[] args) throws Exception {
        //1.创建DatagramSocket对象，用来接收数据，还需要指定端口号
        DatagramSocket socket = new DatagramSocket(8585);

        //2.需要创建DatagramPacket对象，用来打包接收到的数据
        byte[] bys = new byte[1024 * 64];
        DatagramPacket packet = new DatagramPacket(bys, bys.length);

        //3.循环接受
        while (true) {
            socket.receive(packet);

            //获取接收到的字节个数
            int length = packet.getLength();

            //解码数据
            String msg = new String(bys, 0, length);
            System.out.println(packet.getSocketAddress() + " 客户端发送的数据：" + msg);
        }
    }
}
